//
//  CZStatusRetweetView.swift
//  WeiBo
//
//  Created by Apple on 16/9/6.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import YYText

//  转发微博视图
class CZStatusRetweetView: UIView {
    //  转发视图底部约束
    var retweetBottomConstraint: Constraint?
    
    //  数据绑定模型
    var statusViewModel: CZStatusViewModel? {
        didSet {
            
            //contentLabel.text = statusViewModel?.retweetContent
            contentLabel.attributedText = statusViewModel?.retweetAttributedString
            
            //  卸载上次底部约束
            retweetBottomConstraint?.uninstall()
            //  判断是否有转发配图
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  有配图
                //  绑定数据，更新约束，显示配图视图
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                   retweetBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  绑定数据
                pictureView.picUrls = picUrls
                
            } else {
                //  没有配图
                //  更新约束，隐藏配图视图
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    retweetBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
            
        }
    }
    
    //  转发微博label
    private lazy var contentLabel: YYLabel = {
//        let label = UILabel(fontSize: 13, textColor: UIColor.lightGray)
        let label = YYLabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        return label
    }()
    
    //  转发的配图信息
    private lazy var pictureView: CZStatusPictureView = {
        let view = CZStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.top.equalTo(self).offset(HomeCellMargin)
            //make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            make.left.equalTo(contentLabel)
            
        }
        
        //  关键约束
        self.snp_makeConstraints { (make) in
            retweetBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
        //  点击高亮链接的闭包
        contentLabel.highlightTapAction = { (containerView, text, range, rect)->() in
            
            
            let result = (text.string as NSString).substring(with: range)
            if result.hasPrefix("http") {
                print("我是链接")
            } else {
                print(result)
            }
            
        }

        
    }
    

}
